package com.example.apitesttool.service;

import dev.langchain4j.agent.tool.P;
import dev.langchain4j.agent.tool.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * HTTP 请求工具类
 * 
 * 为大模型提供 HTTP 请求执行能力的工具集合。
 * 这些工具将被 LangChain4j 的 AI Agent 调用，用于执行实际的 API 测试。
 * 
 * 特性：
 * - 支持所有常见的 HTTP 方法
 * - 自动注入固定参数
 * - 提供详细的请求响应信息
 * - 错误处理和异常捕获
 * 
 * @author liuxy
 * @version 1.0.0
 */
@Component
public class HttpRequestTools {
    
    private final HttpRequestExecutor httpRequestExecutor;

    @Autowired
    public HttpRequestTools(HttpRequestExecutor httpRequestExecutor) {
        this.httpRequestExecutor = httpRequestExecutor;
    }

    @Tool("执行 HTTP GET 请求。用于获取数据或查询信息。")
    public String executeGetRequest(@P("baseUrl") String baseUrl, @P("path") String path, @P("支持格式：key1=value1,key2=value2 或 key1:value1,key2:value2") String queryParams, String headers) {
        Map<String, String> queryParamsMap = parseKeyValueString(queryParams);
        Map<String, String> headersMap = parseKeyValueString(headers);
        
        return httpRequestExecutor.executeGet(baseUrl, path, queryParamsMap, headersMap);
    }

    @Tool("执行 HTTP POST 请求。用于创建新资源或提交数据。")
    public String executePostRequest(String baseUrl, String path, String requestBodyJson, String queryParams, String headers) {
        Map<String, String> queryParamsMap = parseKeyValueString(queryParams);
        Map<String, String> headersMap = parseKeyValueString(headers);
        
        return httpRequestExecutor.executePost(baseUrl, path, requestBodyJson, queryParamsMap, headersMap);
    }

    @Tool("执行 HTTP PUT 请求。用于更新现有资源或创建资源。")
    public String executePutRequest(String baseUrl, String path, String requestBodyJson, String queryParams, String headers) {
        Map<String, String> queryParamsMap = parseKeyValueString(queryParams);
        Map<String, String> headersMap = parseKeyValueString(headers);
        
        return httpRequestExecutor.executePut(baseUrl, path, requestBodyJson, queryParamsMap, headersMap);
    }

    @Tool("执行 HTTP DELETE 请求。用于删除资源。")
    public String executeDeleteRequest(String baseUrl, String path, @P("支持格式：key1=value1,key2=value2 或 key1:value1,key2:value2")String queryParams, String headers) {
        Map<String, String> queryParamsMap = parseKeyValueString(queryParams);
        Map<String, String> headersMap = parseKeyValueString(headers);
        
        return httpRequestExecutor.executeDelete(baseUrl, path, queryParamsMap, headersMap);
    }

    @Tool("执行 HTTP PATCH 请求。用于部分更新资源。")
    public String executePatchRequest(String baseUrl, String path, String requestBodyJson, String queryParams, String headers) {
        Map<String, String> queryParamsMap = parseKeyValueString(queryParams);
        Map<String, String> headersMap = parseKeyValueString(headers);
        
        return httpRequestExecutor.executePatch(baseUrl, path, requestBodyJson, queryParamsMap, headersMap);
    }

    /**
     * 解析键值对字符串为 Map
     * 支持格式：key1=value1,key2=value2 或 key1:value1,key2:value2
     * 
     * @param keyValueString 键值对字符串
     * @return 解析后的 Map
     */
    private Map<String, String> parseKeyValueString(String keyValueString) {
        Map<String, String> result = new HashMap<>();
        
        if (keyValueString == null || keyValueString.trim().isEmpty()) {
            return result;
        }
        
        String[] pairs = keyValueString.split(",");
        for (String pair : pairs) {
            String trimmedPair = pair.trim();
            if (trimmedPair.isEmpty()) {
                continue;
            }
            
            // 支持 = 或 : 作为分隔符
            String[] keyValue = trimmedPair.split("[=:]", 2);
            if (keyValue.length == 2) {
                String key = keyValue[0].trim();
                String value = keyValue[1].trim();
                if (!key.isEmpty()) {
                    result.put(key, value);
                }
            }
        }
        
        return result;
    }
}

